﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Main_Inventory_PurchasedReceivedDetail : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string Po_id = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["POID"] != null)
        {
            Po_id = Request.QueryString["POID"].ToString();
        }
        if (!IsPostBack)
        {
            bindlabel(Po_id);
            bindgrid();
            bindsuppliername(Po_id);

            GridViewRow row = GridDisplay.FooterRow;
            if (row.Cells[10].Controls[0] != null)
            {
                HidGrdTotal.Value = row.Cells[10].Controls[0].ClientID;
            }
        }
    }

    protected void bindlabel( string Po_id)
    {
        string sql_select = "select POID,PoPrefix,PoSuffix,PoVoucherNo, LID from ACC_POMAIN where POID = "+ Po_id;
        DataTable DT= DU.getdata(sql_select);
        if (DT.Rows.Count > 0)
        {
            string vouchermain = "";
            string prefix = "";
            string vono = "";
            string suffix = "";

            if (DT.Rows[0]["PoPrefix"].ToString() != string.Empty)
            {
                prefix = DT.Rows[0]["PoPrefix"].ToString() + "/";
            }
            if (DT.Rows[0]["PoVoucherNo"].ToString() != string.Empty)
            {
                vono = DT.Rows[0]["PoVoucherNo"].ToString();
            }
            if (DT.Rows[0]["PoSuffix"].ToString() != string.Empty)
            {
                suffix = "/" + DT.Rows[0]["PoSuffix"].ToString();
            }
            vouchermain = prefix + vono + suffix;
            LblPurchaseNumber.Text = vouchermain;
        }
    }

    protected void bindsuppliername(string Po_id)
    {
        string sql = "select ACC_LEDMAST.LNAME from ACC_POMAIN inner join ACC_LEDMAST on ACC_LEDMAST.LID=ACC_POMAIN.LID and ACC_POMAIN.POID =" + Po_id ;
        DataTable DT = DU.getdata(sql);
        if (DT.Rows.Count > 0)
        {
            LblSupplierName.Text = DT.Rows[0][0].ToString();
        }

    }
    protected void bindgrid()
    {
        string sql = "select * from ACC_PODETAILS where poid = "+ Po_id;
        DataTable dt = DU.getdata(sql);
        DataTable details = new DataTable();
        details.Columns.Add("id", typeof (string));
        details.Columns.Add("ItemName", typeof(string));
        details.Columns.Add("UomName", typeof (string));
        details.Columns.Add("Quantity", typeof(double));
        
      
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow DR = details.NewRow();

            
            DR["id"] = dt.Rows[i]["id"].ToString();
            DR["Quantity"] = Convert.ToDouble(dt.Rows[i]["ItemQty"].ToString());

            
            string Itemid = dt.Rows[i]["ItemId"].ToString();
            DR["ItemName"] = DU.getdata("Select ITMDESC from IVR_ITEM where ITMID=" + Itemid).Rows[0][0].ToString();

            string PRIMARY_Unit = dt.Rows[i]["UOMID"].ToString();
            DR["UOMName"] = DU.getdata("select UOMNAME from IVR_UOM where UOMID=" + PRIMARY_Unit).Rows[0][0].ToString();

            details.Rows.Add(DR);
        }
       
       HidGrdRowCount.Value = details.Rows.Count.ToString();
        GridDisplay.DataSource = details;
        GridDisplay.DataBind(); 
       
    }
   
    protected void GridDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[9].Text = "<div align='right'>Grand Total</div>";
            e.Row.Cells[9].Style.Add("font-weight", "bold");
            TextBox txt = new TextBox();
            txt.ID = "TxtGrandTotal";
            txt.Attributes.Add("class", "inputnumber");
            txt.Width = Unit.Pixel(80);
            txt.Text = "0";
            txt.Enabled = false;
           
            e.Row.Cells[10].Style.Add("text-align", "center");
            e.Row.Cells[10].Controls.Add(txt);
            
        }
    }

    protected void BtnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("PurchasedReceived.aspx");
    }
}
